import { PetExploreConfig } from "../../../resource/PetExploreConfig";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { MsgManager } from "../../../manager/MsgManager";
import { Language } from "../../../../../extend/language/Language";
import { Cache } from "../../../cache/Cache";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { DataEvent } from "../../../events/DataEvent";
import { EventName } from "../../../mvc/EventName";
import { SPetExploreInfo } from "../../../../../Message/Public/SPetExploreInfo";
import { ToolTipItem } from "../../../../../com/mui/manager/ToolTipItem";
import { PetExploreRoadblock } from "./PetExploreRoadblock";
import { DisplayUtil } from "../../../../common/DisplayUtil";
import { PetExploreConfigInfo } from "../../../resource/info/PetExploreConfigInfo";
import { GameConst } from "../../../../component/gconst/GameConst";
import { LoaderManager } from "../../../../../com/gengine/resource/LoaderManager";
import { PetExploreUtil } from "./info/PetExploreUtil";
import { SWFInfo } from "../../../../../com/gengine/resource/info/SWFInfo";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { PetExploreAreaConfig } from "../../../resource/PetExploreAreaConfig";
import { PetExploreAreaInfo } from "../../../resource/info/PetExploreAreaInfo";
import { ItemData } from "../../../resource/info/ItemData";
import { ItemsUtil } from "../../shortcuts/item/ItemsUtil";
import { _areaConfig } from "../../../../../this/_areaConfig";
import { _preAreaConfig } from "../../../../../this/_preAreaConfig";
type int = number;
//class PetExploreArea
    
    export  class PetExploreArea /* flash.display.Sprite */
    {
        constructor(arg1: int=1, arg2: Object /* flash.display.Sprite */=null)
        {
            
            this._tollgateArray = [];
            this._rbArray = [];
            this._bmpDict = new Map<any, any> /* flash.utils.Dictionary */();
            this._parentSp = arg2;
            this._index = arg1;
            this._tollgateArray = PetExploreConfig.instance.getExploreConfigByArea(this._index);
            this.init();
            NetDispatcher.addCmdListener(ServerCommand.PetExploreInfoCommand, this.onPetExploreInfoCommandHandler);
            return;
        }

        /* internal  */onThisClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (this._tollgateArray.length == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54459));
                return;
            }
            var loc1=Cache.instance.petExplore.getExploreAreaIndex();
            if (!this._isEnable) 
            {
                if (this._preAreaConfig && this._preAreaConfig.area > loc1) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(54460));
                }
                else 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreEnableWindowOpen, {"cArea": _areaConfig, "pArea": _preAreaConfig}));
                }
            }
            return;
        }

        /* internal  */onPetExploreInfoCommandHandler(arg1: SPetExploreInfo): void
        {
            var loc1=Cache.instance.petExplore.getExploreAreaIndex();
            if (this._areaConfig.area <= loc1) 
            {
                this.isEnable = true;
            }
            else 
            {
                this.isEnable = false;
            }
            this.updateAreaRbStatus();
            this._mapTipPanel.toolTipData = this.getToolTipData();
            return;
        }

        /* internal  */init(): void
        {
            this._mapTipPanel = new ToolTipItem();
            this.addChild(this._mapTipPanel);
            this._mapSprite = new Object /* flash.display.Sprite */();
            this._mapTipPanel.addChild(this._mapSprite);
            this._enableBitmap = new Object /* flash.display.Bitmap */();
            this._mapSprite.addChild(this._enableBitmap);
            this._mapSprite.mouseEnabled = false;
            this.createTollgate();
            this._hitSprite = new Object /* flash.display.Sprite */();
            this._hitSprite.visible = false;
            this._hitSprite.mouseEnabled = false;
            this.addChild(this._hitSprite);
            hitArea = this._hitSprite;
            this._mapTipPanel.hitArea = this._hitSprite;
            this.addEventListener(flash.events.MouseEvent.MOUSE_OVER, this.onMouseOverHandler);
            this.addEventListener(flash.events.MouseEvent.MOUSE_OUT, this.onMouseOutHandler);
            this.addEventListener(flash.events.MouseEvent.CLICK, this.onThisClickHandler);
            return;
        }

        /* internal  */createTollgate(): void
        {
            var loc1=null;
            var loc2=null;
            var loc5=0;
            var loc3=this._rbArray.length;
            var loc4=this._tollgateArray.length;
            loc5 = 0;
            while (loc5 < loc3) 
            {
                loc2 = this._rbArray[loc5] as PetExploreRoadblock;
                DisplayUtil.removeMe(loc2);
                ++loc5;
            }
            loc5 = 0;
            while (loc5 < loc4) 
            {
                loc1 = this._tollgateArray[loc5] as PetExploreConfigInfo;
                if (loc3 > loc5) 
                {
                    loc2 = this._rbArray[loc5] as PetExploreRoadblock;
                }
                else 
                {
                    loc2 = new PetExploreRoadblock(this._parentSp);
                    this._rbArray.push(loc2);
                }
                loc2.exploreConfig = loc1;
                loc2.x = loc1.x;
                loc2.y = loc1.y;
                this.addChild(loc2);
                ++loc5;
            }
            this.updateAreaRbStatus();
            return;
        }

        /* internal  */updateAreaRbStatus(): void
        {
            var loc1=null;
            var loc3=0;
            var loc4=false;
            var loc5=0;
            var loc6=0;
            var loc7=0;
            var loc2=Cache.instance.petExplore.petExploreInfo;
            if (loc2) 
            {
                loc3 = loc2.currentLevel;
                loc4 = loc2.isInExplore;
                loc5 = GameConst.PetExploreMaxTimesPerDay - loc2.times;
                loc6 = loc2.autoMaxLevel;
            }
            var loc8=0;
            while (loc8 < this._rbArray.length) 
            {
                loc1 = this._rbArray[loc8] as PetExploreRoadblock;
                loc7 = loc1.exploreConfig.level;
                if (loc4) 
                {
                    if (loc7 < loc3) 
                    {
                        loc1.status = 0;
                    }
                    else if (loc7 != loc3) 
                    {
                        if (loc7 <= loc6) 
                        {
                            loc1.status = 3;
                        }
                        else 
                        {
                            loc1.status = 2;
                        }
                    }
                    else 
                    {
                        loc1.status = 1;
                    }
                }
                else if (Cache.instance.petExplore.isAgainExplore() && loc5 > 0 && loc7 == loc3) 
                {
                    loc1.status = 1;
                }
                else if (loc7 < loc3) 
                {
                    loc1.status = 0;
                }
                else if (loc7 <= loc6) 
                {
                    loc1.status = 3;
                }
                else 
                {
                    loc1.status = 2;
                }
                loc1.updateIsCanExplore();
                ++loc8;
            }
            return;
        }

        public get isEnable(): Boolean
        {
            return this._isEnable;
        }

        public set index(arg1: int)
        {
            this._index = arg1;
            this._tollgateArray = PetExploreConfig.instance.getExploreConfigByArea(this._index);
            this.createTollgate();
            return;
        }

        public set isEnable(arg1: Boolean)
        {
            var value: Boolean;

            var loc1;
            value = arg1;
            this._isEnable = value;
            this._enableBitmap.bitmapData = null;
            LoaderManager.instance.load(PetExploreUtil.getAreaImageName(this._index, value), (arg1: SWFInfo): void
            {
                var loc3=0;
                _bmpDict[value] = arg1.bitmapData;
                _enableBitmap.bitmapData = arg1.bitmapData;
                _hitSprite.graphics.clear();
                var loc1=arg1.bitmapData;
                _hitSprite.graphics.beginFill(0);
                _hitSprite.cacheAsBitmap = true;
                var loc2=0;
                while (loc2 < loc1.width) 
                {
                    loc3 = 0;
                    while (loc3 < loc1.height) 
                    {
                        if (loc1.getPixel32(loc2, loc3)) 
                        {
                            _hitSprite.graphics.drawRect(loc2, loc3, 1, 1);
                        }
                        ++loc3;
                    }
                    ++loc2;
                }
                _hitSprite.graphics.endFill();
                return;
            })
            return;
        }

        public get status(): int
        {
            return this._status;
        }

        public set status(arg1: int)
        {
            this._status = arg1;
            return;
        }

        /* internal  */onMouseOverHandler(arg1: Object /* flash.events.Event */): void
        {
            this._enableBitmap.filters = [FilterConst.worldMapFilter];
            return;
        }

        /* internal  */onMouseOutHandler(arg1: Object /* flash.events.Event */): void
        {
            this._enableBitmap.filters = [];
            return;
        }

        public getToolTipData()
        {
            var loc1=Cache.instance.petExplore.getExploreAreaIndex() > this._areaConfig.area;
            if (this._isEnable) 
            {
                if (loc1) 
                {
                    this._tip = Language.getStringByParam(54455, this._areaName);
                }
                else 
                {
                    this._tip = Language.getStringByParam(54456, this._areaName);
                }
            }
            else 
            {
                this._preAreaConfig = PetExploreAreaConfig.instance.getAreaConfigByArea((this._areaConfig.area - 1));
                if (this._preAreaConfig) 
                {
                    this._preAreaName = this._preAreaConfig.name;
                }
                if (this._tollgateArray.length > 0) 
                {
                    this._tip = Language.getStringByParam(54457, this._areaConfig.costItemAmount, this._costItemName, this._costItemName);
                }
                else 
                {
                    this._tip = Language.getString(54458);
                }
            }
            return this._tip;
        }

        public get areaConfig(): PetExploreAreaInfo
        {
            return this._areaConfig;
        }

        public set areaConfig(arg1: PetExploreAreaInfo)
        {
            this._areaConfig = arg1;
            if (arg1) 
            {
                this._areaName = arg1.name;
                if (arg1.costItemCode != 0) 
                {
                    this._costItemData = new ItemData(arg1.costItemCode);
                    this._costItemName = ItemsUtil.getItemName(this._costItemData);
                }
                this._mapTipPanel.toolTipData = this.getToolTipData();
            }
            return;
        }

        public setToolTipData(): void
        {
            this._mapTipPanel.toolTipData = this.getToolTipData();
            return;
        }

        /* internal */ /* var */_mapTipPanel: ToolTipItem;

        /* internal */ /* var */_mapSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_pointSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_enableBitmap: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_hitSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_status: int;

        /* internal */ /* var */_isEnable: Boolean;

        /* internal */ /* var */_index: int;

        /* internal */ /* var */_tollgateArray: Array<any>;

        /* internal */ /* var */_rbArray: Array<any>;

        /* internal */ /* var */_tip: string;

        /* internal */ /* var */_areaConfig: PetExploreAreaInfo;

        /* internal */ /* var */_areaName: string;

        /* internal */ /* var */_bmpDict: Map<any, any> /* flash.utils.Dictionary */;

        /* internal */ /* var */_preAreaConfig: PetExploreAreaInfo;

        /* internal */ /* var */_preAreaName: string="";

        /* internal */ /* var */_costItemData: ItemData;

        /* internal */ /* var */_parentSp: Object /* flash.display.Sprite */;

        /* internal */ /* var */_costItemName: string;
    }
